package net.liteheaven.dtimer

import android.app.Application
import android.content.*
import android.os.Handler
import android.os.IBinder
import android.os.Message
import net.liteheaven.dtimer.misc.CommonService
import net.liteheaven.dtimer.misc.CommonService.ACTION_REBORN
import net.liteheaven.dtimer.timer.DTimerCallback
import net.liteheaven.dtimer.timer.DTimerManager
import net.liteheaven.dtimer.timer.WakeLockUtil

/**
 * Created by LiangYu on 2018/7/30.
 */
class DTimerApp : Application(), DTimerCallback {

    private lateinit var handle: MyHandler

    override fun onCreate() {
        super.onCreate()
        XLog.i("DTimerApp::onCreate()")

        // 测试静态注册接收器接收普通广播
        // 结论：静态注册接收器，普通广播依然有效
        /*handle = MyHandler()
        handle.app = this
        handle.sendEmptyMessageDelayed(100, 5000)*/

        // 测试定时功能
        DTimerManager.attach(this)
        DTimerManager.registerCallback(this)
        //DTimerManager.addTimer(1,1000, false)

        val intentFilter = IntentFilter()
        intentFilter.addAction(ACTION_REBORN)
        registerReceiver(globalReceiver, intentFilter)
        startService(Intent(this@DTimerApp, CommonService::class.java))
    }

    override fun onTerminate() {
        super.onTerminate()
        DTimerManager.detach(this)
    }

    override fun onTimer(timerId: Int) {
        if (timerId == 1){
            val lock = WakeLockUtil.lock(this@DTimerApp, "DTimerApp")
            XLog.i("fired!!!")
            lock.release()
        }
    }

    private class MyHandler : Handler() {

        lateinit var app : Application

        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (msg != null){
                when(msg.what){
                    // 给静态注册的接收器发送广播
                    100->{
                        sendEmptyMessageDelayed(100, 5000)
                        if (app != null){
                            app!!.applicationContext.sendBroadcast(Intent("net.liteheaven.dtimer.ACTION_S"))
                        }
                    }
                    // CommonService死了，扶起来
                    200->{
                        app.bindService(Intent(app, CommonService::class.java), cnn, 0)
                    }
                }
            }
        }
    }

    private val globalReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent!=null){
                when(intent.action){
                    ACTION_REBORN->{
                        bindService()
                    }
                }
            }
        }
    }

    object cnn : ServiceConnection {
        override fun onServiceDisconnected(name: ComponentName?) {
            XLog.i("onServiceDisconnected")
        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            XLog.i("onServiceConnected")
        }

    }

    private fun bindService(){
        val result = bindService(Intent(this@DTimerApp, CommonService::class.java), cnn, BIND_AUTO_CREATE)
    }
}
